package com.system.couponserver.service.impl;

import com.system.couponserver.mapper.UserCouponMapper;
import com.system.couponserver.pojo.Coupon;
import com.system.couponserver.pojo.User;
import com.system.couponserver.pojo.User_Coupon;
import com.system.couponserver.service.UserCouponService;
import com.system.couponserver.utils.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class UserCouponServiceImpl implements UserCouponService {

    @Autowired(required = false)
    private UserCouponMapper userCouponMapper;
    /**
     * 全部查询
     * @param currentPsge
     * @param pageSize
     * @return
     */
    @Override
    public ResultData query01(int currentPsge, int pageSize) {
        int srartParam = (currentPsge-1)*pageSize;
        System.out.println(666);
        ResultData resultData = new ResultData();
        List<User> list = userCouponMapper.query01(srartParam,pageSize);
        resultData.setData(list);
        // 返回总条数
        resultData.setCount(totalSize());
        //返回当前页码数
        resultData.setPages(currentPsge);
        //返回当前显示多少条
        resultData.setLimit(pageSize);
        return resultData;
    }

    /**
     * 条件查询 通过优惠卷id 优惠卷状态 查询
     */
    @Override
    public ResultData query03(User user) {
        int currentpage = user.getCurrentPsge();
        int pageSize = user.getPageSize();
        int srartParam = (currentpage-1)*pageSize;
        user.setCurrentPsge(srartParam);
        ResultData resultData = new ResultData();
        List<User> list = userCouponMapper.query03(user);
        System.out.println(srartParam);
        user.setCurrentPsge(0);
        user.setPageSize(totalSize());
        List<User> list1 = userCouponMapper.query03(user);
        resultData.setData(list);
//        // 返回总条数
        resultData.setCount(list1.size());
//        //返回当前页码数
        resultData.setPages(currentpage);
//        //返回当前显示多少条
        resultData.setLimit(pageSize);
        return resultData;
    }

    /**
     * 领取优惠卷
     * @param user_coupon
     * @return
     */
    @Override
    @Transactional
    public int insert(User_Coupon user_coupon) {
        return userCouponMapper.insert(user_coupon);
    }

    /**
     * 返回总条数
     * @return
     */
    private int totalSize() {
        return userCouponMapper.query02();
    }
}
